package com.thinkgsp.report;

import java.math.BigDecimal;

/**
 * 报表打印工具类
 * @author evan
 *
 */
public class ReportUtils {

	/**
	 * 转换金额成大写
	 * @param money
	 * @return
	 */
	public static String convertMoneyToChinaUpper(BigDecimal money) {
		if(money == null) {
			return "￥空";
		}
		money = money.setScale(2, BigDecimal.ROUND_HALF_UP);
		if(BigDecimal.ZERO.compareTo(money) == 0) {
			return "￥零元整";
		}
        //18个单位
        String[] StrScale ={ "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
        //10个数字
        String[] StrNumber ={ "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

        String result = "";				//结果
        String temp = money.toString();	//临时结果
        int decimalPoint = 0;			//小数点补偿
        boolean integral = false;		//是否整数
        if(temp.indexOf('.') == -1) {
        	integral = true;
        	decimalPoint = 2;
        } else {
        	decimalPoint = temp.indexOf('.') + 3 - temp.length();
        }
        temp = temp.replace(".", "").replace("-", "");	//去除小数点
        for(int i = temp.length();i > 0; i--) {
        	int numindex = temp.length()-i;		//取数位置
        	String num = temp.substring(numindex, numindex+1);//取数值
        	int number = Integer.parseInt(num);//取数值的int类型
        	result += StrNumber[number];	   //取数字的大写
        	result += StrScale[i-1+decimalPoint];//取数值的位数大写
        }
        //替换零
        result = result.replace("零角零分", "整");
        result = result.replace("零分", "");
        result = result.replace("零角", "零");
        result = result.replace("零仟", "零");
        result = result.replace("零佰", "零");
        result = result.replace("零拾", "零");
        result = result.replace("零零零零元", "元");
        result = result.replace("零零零零万", "");
        result = result.replace("零零零零亿", "");
        result = result.replace("零零零零兆", "");
        result = result.replace("零零零", "零");
        result = result.replace("零零", "零");
        result = result.replace("零元", "元");
        result = result.replace("零万", "万");
        result = result.replace("零亿", "亿");
        result = result.replace("零兆", "兆");
        if(integral) {
        	result += "整";
        }
        //去除第一位的'元'与'零'
        if("元".equals(result.substring(0, 1))) {
        	result = result.substring(1, result.length());
        }
        if("零".equals(result.substring(0, 1))) {
        	result = result.substring(1, result.length());
        }
        if(BigDecimal.ZERO.compareTo(money) > 0) {
        	result = "￥负" + result;
        } else {
        	result = "￥" + result;
        }
		return result;
	}
}
